<?php

namespace app\models;

use yii\db\ActiveRecord;
use Yii;

class login extends ActiveRecord{

	public static function tableName(){
	       return "{{%user}}";
	}

	public function rules(){
           return[
	        [['openid','password'],'required','message'=>'账号或密码不能为空~'],
	        ['openid','string','length' =>[6,17],'message' => '账号不能少于2位~'],
	        ['password','string','length' => [6,17],'message' => '密码长度介于4-12位之间~'],
           ];
	}
       //验证功能
	public function add($data){
         if($data){
          $openid = $data['formUsername'];
          $password = $data['formPassword'];        
 
          $sql = "select id from wall_user where openid='$openid'";
 
          //查询没有结果则返回null 空
          $value = Yii::$app->db->createCommand($sql)->queryOne();
         //账号不存在
         if($value == null){
              $sex = rand(1,2); 
              //输入到数据库
              $this->openid = $openid;
              $this->password = $password;
              $this->sex = $sex;
              $result = $this->save();
              if($result){
                  $error = 0 ;
                  return $error;
              } else{
                  $error = $this->getErrors();
                  return $error;
              }
 
	} else{
                   //账号存在,比对密码
		    $sqlt = "select * from wall_user where openid='$openid' and password='$password'";
                    $post = Yii::$app->db->createCommand($sqlt)->queryOne();//返回值为空或非空 
		    if($post!=null){
		     // $error = '配对成功'.$post.$sqlt;
		        $error = 0;
                        return $error;
		    } else{
		        $error = $this->getErrors();
		        return $error;
		    }
            
		  } 

		}else{
                      $error = 2;
                      return $error;
               }   
	 }
          
          public function mod($data){
              if($data){
                $sex = $data['formSex'];
                $nickname = $data['formName'];
                $openid = $data['username'];
                $command = Yii::$app->db->createCommand("UPDATE wall_user SET sex = $sex,nickname='$nickname' WHERE openid = '$openid'" );
                $result = $command->execute();
                if($result){
                     $error = 0;
                     return $error; 
               }else{  
                  //   $error = $sex; 
                     $error = $this->getErrors();
                     return $error; 
                }       
              }       
              else{   
                     $error = 2;
                     return $error; 
              }       
         }




          public function mode($data){
               if($data){
              //处理数据
              $sex = $data['formSex'];
              $nickname = $data['formName'];
              $openid = $data['username'];
               //输入数据库&&更新user表
              
                 $update= login::findOne($sex);
              //    var_dump($user);
                 // $login = new login();
                 // $user = $login->find()->where(['openid' => '$openid'])->one();
		  $update->sex = $sex;
		   $update->nickname = $nickname;
		   $result= $update->save();

                // $sql = "select * from wall_user where openid = '{$openid}'";
		//   $value = Yii::$app->db->createCommand($sql)->queryOne();
		/**   if(!is_null($value)){
		       $this->sex = $sex;
		       $this->nickname = $nickname;
		       $result = $this->save();*/
		   
                     if($result){
                     $error = 0;
                     return $error; 
               }else{  
                     $error = $sex;
                  //   $error = $this->getErrors();
                     return $error; 
                }       
              }
              else{  
                     $error = 2;
                     return $error; 
              }       
         }
}

